<?php  # Script -- isimg2.php

/* Author @ Huoty
 *   Date @ 2015-11-25 16:42:38
 *  Brief @ 
 */

//判断上传的是不是图片 
function  isImg($fileName)
{ 
    $file     = fopen($fileName, "rb"); 
    $bin      = fread($file, 2);  // 只读2字节 

    fclose($file); 
    $strInfo  = @unpack("C2chars", $bin); 
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']); 
    $fileType = ''; 

    if($typeCode == 255216 /*jpg*/ || $typeCode == 7173 /*gif*/ || $typeCode == 13780 /*png*/) 
    { 
        return $typeCode; 
    }
    else
    { 
        // echo '"仅允许上传jpg/jpeg/gif/png格式的图片！'; 
        return false; 
    } 
} 

if (isImg("1.jpg"))
{
    echo "是图片";
}
else
{
    echo "不是图片";
}

?>
